package com.zego.wawaji.ui.store.order;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.log.PToast;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.store.order.bean.Order;

import java.util.List;

/**
 * Created by Su on 2018/1/9.
 */
public class StoreOrderAdapter extends ExBaseAdapter<Order> {

    public StoreOrderAdapter(Context context, List<Order> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.store_order_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.marginBottom.setVisibility(View.GONE);
        holder.marginTop.setVisibility(View.VISIBLE);
        if (position == 0) {
            holder.marginTop.setVisibility(View.GONE);
        }
        if (position == getList().size() - 1) {
            holder.marginBottom.setVisibility(View.VISIBLE);
        }

        final Order info = getItem(position);

        ImageLoader.loadRoundAvatar(getContext(), info.getGoodsImg(), holder.order_icon);
        holder.wawa_name.setText(info.getGoodsTitle());
        holder.coin_info.setText(String.valueOf(info.getGoodsPrize()));
        int amount = info.getGoodsAmount();
        holder.order_num.setText(String.valueOf(amount));
        holder.order_desc.setText("共" + amount + "件商品 合计" + amount * info.getGoodsPrize() + "金币");
        holder.order_status.setText(isFinish(info.getOrderStatus()) ? "订单已完成" : "订单未完成");
        holder.order_status.setTextColor(isFinish(info.getOrderStatus()) ?
                getContext().getResources().getColor(R.color.text_gray) : getContext().getResources().getColor(R.color.text_orange));
        holder.btn_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModuleMgr.getWawaMgr().reqTransport(info.getTid(), null);
            }
        });

        holder.delete.setVisibility(isFinish(info.getOrderStatus()) ? View.VISIBLE : View.GONE);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModuleMgr.getWawaMgr().reqUpdateStatus(info.getId(), -2, null);
            }
        });

        return convertView;
    }

    private boolean isFinish(int order) {
        return order >= 5;
    }

    private class MyViewHolder {
        private View marginTop, marginBottom;
        private ImageView order_icon, delete;
        private TextView wawa_name, coin_info, order_desc, order_num, order_status;
        private RelativeLayout btn_check;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            marginTop = convertView.findViewById(R.id.margin_top);
            marginBottom = convertView.findViewById(R.id.margin_bottom);
            order_icon = (ImageView) convertView.findViewById(R.id.order_icon);
            wawa_name = (TextView) convertView.findViewById(R.id.wawa_name);
            coin_info = (TextView) convertView.findViewById(R.id.coin_info);
            btn_check = (RelativeLayout) convertView.findViewById(R.id.order_logistics);
            order_desc = (TextView) convertView.findViewById(R.id.order_desc);
            order_num = (TextView) convertView.findViewById(R.id.order_num);
            order_status = (TextView) convertView.findViewById(R.id.order_status);
            delete = (ImageView) convertView.findViewById(R.id.delete);
        }
    }
}
